package com.jiao.store.service.impl;

import com.jiao.store.controller.ex.AccessDeniedException;
import com.jiao.store.controller.ex.AddressNotFoundException;
import com.jiao.store.controller.ex.DelException;
import com.jiao.store.entity.Address;
import com.jiao.store.mapper.AddressMapper;
import com.jiao.store.mapper.DistrictsMapper;
import com.jiao.store.service.AddressService;
import com.jiao.store.service.DistrictsService;
import com.jiao.store.service.ex.AddressCountLimitException;
import com.jiao.store.service.ex.InsertServiceException;
import com.jiao.store.service.ex.UpdateException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
public class AddressServiceImpl implements AddressService {
     @Autowired
       private AddressMapper addressMapper;
     @Autowired
     private DistrictsService districtsService;

      @Value("${user.address.max-count}")
     private  Integer maxCount;


    @Override
    public void AddNewAddress(Integer uid, String username, Address address) {
         //判断是否上线
        Integer count = addressMapper.countByUid(uid);
        if (count>=maxCount){
            throw  new AddressCountLimitException("用户地址已上线");
        }
        //补全数据
        address.setUid(uid);
        Integer isDefault =count ==0 ? 1:0;
        address.setIsDefault(isDefault);
        address.setCreatUser(username);
        address.setModifiedUser(username);
        address.setCreatTime(new Date() );
        address.setModifiedTime(new Date());
        //补全地址
        String provinceName = districtsService.getNameByCode(address.getProvinceCode());
        String cityName = districtsService.getNameByCode(address.getCityCode());
        String areaName = districtsService.getNameByCode(address.getAreaCode());
           address.setProvinceName(provinceName);
           address.setCityName(cityName);
           address.setAreaName(areaName);
        Integer rows = addressMapper.insert(address);
        if (rows !=1){
            throw  new InsertServiceException("新增地址失败");
        }
    }

    @Override
    public List<Address> getByUid(Integer uid) {

        return addressMapper.findByUid(uid);
    }

    @Override
    public void setDefault(Integer aid, Integer uid, String username) {
        Address result = addressMapper.findByAid(aid);
        if (result==null){
            throw  new AddressNotFoundException("没有找到收获地址");
        }
        //检测当前收获地址的归属
        if (!result.getUid().equals(uid)){
            throw  new AccessDeniedException("非法访问！");

        }
        //先将所有收获地址改成非默认
        Integer rows = addressMapper.updateNonDefault(uid);
        if (rows< 1){
                throw  new UpdateException("更新数据产生未知异常");
        }
        //将用户选中的某条地址设置为默认收获的地址
            rows=  addressMapper.updateDefaultByAid(aid,username,new Date());
        if (rows!= 1){
            throw  new UpdateException("更新数据产生未知异常");
        }


    }

    @Override
    public void delByAid(Integer aid, Integer uid, String username) {
        Address result = addressMapper.findByAid(aid);
        if (result==null){
             throw  new AddressNotFoundException("地址找不到，");
        }
        //检测当前收获地址的归属
        if (!result.getUid().equals(uid)){
            throw  new AccessDeniedException("非法访问！");
        }

        Integer rows = addressMapper.delById(aid);
        if (rows!= 1){
            throw  new DelException("删除数据产生未知异常");
        }

        Integer count = addressMapper.countByUid(uid);
        if (count==0){
            return;
        }
        if (result.getIsDefault()==1){
            //将这条数据中的is_default 字符值设置为1
            Address address = addressMapper.findLastModelFiled(uid);
            rows = addressMapper.updateDefaultByAid(address.getAid(), username, new Date());
        }

        if (rows!=1){
            throw  new UpdateException("更新时产生异常");
        }
    }


    @Override
    public Address findByUid(Integer uid) {
        return null;
    }



    @Override
    public void updateAddress(Address address) {
        Address result = addressMapper.findByAid(address.getAid());
        if (result==null){
            throw new AddressNotFoundException("找不到地址产生异常");
        }
        Integer rows = addressMapper.updateAddress(address);
        if (rows!=1){
            throw  new UpdateException("修改数据产生未知异常");
        }
    }
}
